/**
 * eslint-disable @sap/ui5-jsdocs/no-jsdoc
 */

sap.ui.define([
  "sap/ui/core/UIComponent",
  "sap/ui/Device",
  "firorworkshop185/model/models",
  "sap/ui/model/json/JSONModel",
  "./model/formatter",
  'sap/m/library',
],
  function (UIComponent, Device, models, JSONModel, formatter, mobileLibrary) {
    "use strict";

    var ExpandableTextOverflowMode = mobileLibrary.ExpandableTextOverflowMode;

    return UIComponent.extend("firorworkshop185.Component", {
      formatter: formatter,
      metadata: {
        manifest: "json"
      },

      /**
       * The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
       * @public
       * @override
       */
      init: function () {
        // call the base component's init function
        UIComponent.prototype.init.apply(this, arguments);

        let overflowMode = {
          Popover: ExpandableTextOverflowMode.Popover,
          InPlace: ExpandableTextOverflowMode.InPlace
        }

        var oMod = new JSONModel(overflowMode);
        this.setModel(oMod, 'overflowMode');


        let that = this;
        this.getRouter().attachRouteMatched(function (oEvent) {
          if (oEvent.getParameter("name") === "RouteOverview") {
            // 如果在control组件的js里，这里就是this.bindElement
            // 如果在view对应的controller里，这里就是this.getView().bindElement
            var oModel = that.getModel('info');
            oModel.setData({
              ...oModel.oData,
              getOrderCompleted: false
            });

            that.startGetData();
          }
          if (oEvent.getParameter("name") === "CreateOrder") {
            var oEventBus = sap.ui.getCore().getEventBus();
            oEventBus.publish("createOrderEventChannel", "initInfo");
          }
        });

        // enable routing
        this.getRouter().initialize();

        // set the device model
        this.setModel(models.createDeviceModel(), "device");
      },

      startGetData() {
        this.getData1();
      },


      getData1() {
        var that = this;
        this.getRootControl().bindElement({
          path: "/ENTITY001Set",
          events: {
            change: function (oEvent) {
              var sourceList = oEvent.getSource().getModel().oData;
              // this.setModel(sourceList, "ENTITY001Set");
              this.setModel(formatter.filterSource(sourceList, "ENTITY001Set"), "ENTITY001Set");

              // 在加载完ENTITY001Set的数据后，初始化Overview页的Table
              var oEventBus = sap.ui.getCore().getEventBus();
              // 触发事件
              oEventBus.publish("overViewEventChannel", "initTable", {
                // 传递的数据
                data: "yourEventData"
              });
              oEventBus.publish("overViewEventChannel", "initSearch");
              // 注意此处的this
            }.bind(this),
            dataRequested: function () {
              console.log("Data ENTITY001Set request in progress");
            },
            dataReceived: function () {
              console.log("Data ENTITY001Set request completed");
              that.getData2();
            }
          }
        });
      },

      getData2() {
        var that = this;
        this.getRootControl().bindElement({
          path: "/ENTITY002Set",
          events: {
            change: function (oEvent) {
              var sourceList = oEvent.getSource().getModel().oData;
              this.setModel(formatter.filterSource(sourceList, "ENTITY002Set"), "ENTITY002Set");
              // 注意此处的this
            }.bind(this),
            dataRequested: function () {
              console.log("Data ENTITY002Set request in progress");
            },
            dataReceived: function () {
              console.log("Data ENTITY002Set request completed");
            }
          }
        });
      },
    });
  }
);